package com.glory.wxmemsicpda.fragment

import android.annotation.SuppressLint
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.glory.wxmemsicpda.R
import com.glory.wxmemsicpda.activity.SettingActivity
import com.glory.wxmemsicpda.base.Constant
import com.glory.wxmemsicpda.utils.MyUtils
import com.pda.platform.ui.ui_pdaplatform.adapter.FreeUI_TabIndexGridAdapter
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_BaseFragment
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_TabIndexGridEntity
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_Utils
import kotlinx.android.synthetic.main.fragment_index.*

class IndexFragment : FreeUI_BaseFragment<Any?>() {


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        rvMFG.layoutManager = GridLayoutManager(activity, 3)
        initAll()
    }
    override fun initView() {

    }

    override fun initData() {

    }


    @SuppressLint("SetTextI18n")
    private fun initAll() {
        tvTitle.text = """
            首页
            ${Constant.CURRENT_USER}
            """.trimIndent()
        initMFGArea()
    }

    private fun initMFGArea() {
        val mfg_list = ArrayList<FreeUI_TabIndexGridEntity>()
        var entity = FreeUI_TabIndexGridEntity()
        entity.picName = "aos_start_work"
        entity.text = "开始作业"
        entity.showPlugin= "header,Scan_Start"
        entity.skipClass = "com.glory.wxmemsicpda.activity.SkipDialogActivity"
        mfg_list.add(entity)
        entity = FreeUI_TabIndexGridEntity()
        entity.picName = "aos_start_back"
        entity.text = "批次退站"
        entity.showPlugin= "header,Scan_Back"
        entity.skipClass = "com.glory.wxmemsicpda.activity.SkipDialogActivity"
        mfg_list.add(entity)
        entity = FreeUI_TabIndexGridEntity()
        entity.showPlugin= "header,Scan_End_Package"
        entity.skipClass = "com.glory.wxmemsicpda.activity.SkipDialogActivity"
        entity.picName = "aos_end_work"
        entity.text = "结束作业"
        mfg_list.add(entity)
//        entity = FreeUI_TabIndexGridEntity()
//        entity.showPlugin= "header,Scan_End_Test"
//        entity.skipClass = "com.glory.wxmemsicpda.activity.SkipDialogActivity"
//        entity.picName = "aos_end_work"
//        entity.text = "结束作业(测试)"
//        mfg_list.add(entity)
//        entity = FreeUI_TabIndexGridEntity()
//        entity.showPlugin= "header,Scan_Change_Materials"
//        entity.skipClass = "com.glory.wxmemsicpda.activity.SkipDialogActivity"
//        entity.picName = "aos_change_eqp"
//        entity.text = "材料更换"
//        mfg_list.add(entity)
//        entity = FreeUI_TabIndexGridEntity()
//        entity.showPlugin= "header,Check_Search"
//        entity.skipClass = "com.glory.wxmemsicpda.activity.CheckSearchActivity"
//        entity.picName = "aos_search_check"
//        entity.text = "点检履历"
//        mfg_list.add(entity)
//        entity = FreeUI_TabIndexGridEntity()
//        entity.showPlugin= "header,Check_Inspection"
//        entity.skipClass = "com.glory.wxmemsicpda.activity.CheckInspectionActivity"
//        entity.picName = "aos_new_check_table"
//        entity.text = "巡检"
//        mfg_list.add(entity)
//        entity = FreeUI_TabIndexGridEntity()
//        entity.showPlugin= "header,Check_Table"
//        entity.skipClass = "com.glory.wxmemsicpda.activity.CheckTableActivity"
//        entity.picName = "aos_new_check_table"
//        entity.text = "交接班检"
//        mfg_list.add(entity)
        entity = FreeUI_TabIndexGridEntity()
        entity.showPlugin= "header,DownLoadRecipe"
        entity.skipClass = "com.glory.wxmemsicpda.activity.DownLoadRecipeActivity"
        entity.picName = "recipe_te"
        entity.text = "Recipe下载"
        mfg_list.add(entity)
//        entity = FreeUI_TabIndexGridEntity()
//        entity.showPlugin= "header"
//        entity.skipClass = "com.glory.wxmemsicpda.MainActivity"
//        entity.picName = "aos_change_state"
//        entity.text = "更改设备状态"
//        mfg_list.add(entity)
//        entity = FreeUI_TabIndexGridEntity()
//        entity.showPlugin= "header"
//        entity.skipClass = "com.glory.wxmemsicpda.MainActivity"
//        entity.picName = "aos_unlock"
//        entity.text = "解锁"
//        mfg_list.add(entity)
//        entity = FreeUI_TabIndexGridEntity()
//        entity.showPlugin= "header"
//        entity.skipClass = "com.glory.wxmemsicpda.MainActivity"
//        entity.picName = "aos_search_eqp"
//        entity.text = "清除批次"
//        mfg_list.add(entity)
        entity = FreeUI_TabIndexGridEntity()
        entity.showPlugin= "header,Data_Acquisition"
        entity.skipClass = "com.glory.wxmemsicpda.activity.SkipDialogActivity"
        entity.picName = "aos_buyoff"
        entity.text = "数据采集"
        mfg_list.add(entity)
        entity = FreeUI_TabIndexGridEntity()
        entity.showPlugin= "header,Feeding_Operations"
        entity.skipClass = "com.glory.wxmemsicpda.activity.SkipDialogActivity"
        entity.picName = "aos_ask_repair"
        entity.text = "FG投料操作"
        mfg_list.add(entity)
        val tabIndexGridAdapter = FreeUI_TabIndexGridAdapter(
            setPicToDrawable(mfg_list),
            R.layout.free_ui_glory_adapter_tab_index_grid
        )
        rvMFG.adapter = tabIndexGridAdapter

        ivSetting.setOnClickListener(View.OnClickListener { onViewClicked() })
    }


    @SuppressLint("UseCompatLoadingForDrawables")
    private fun setPicToDrawable(entity: List<FreeUI_TabIndexGridEntity>): List<FreeUI_TabIndexGridEntity> {
        for (og in entity) {
            if (og.picName.isNotEmpty()) {
                val drawable: Drawable = resources
                    .getDrawable(
                        resources
                            .getIdentifier(
                                og.picName,
                                "drawable",
                                activity!!.packageName
                            )
                    )
                og.drawable = drawable
            }
        }
        return entity
    }

    fun onViewClicked() {
        if (MyUtils.isFastClick) {
            showToast("您点击的太快了", false)
            return
        }
        FreeApi_Utils.skipWithExtraIntent("header", activity, SettingActivity::class.java)
    }

    override fun getLayoutResId(): Int {
        return R.layout.fragment_index
    }

    override fun getBaseEntity(): FreeUI_AddViewEntity? {
        return null
    }

}